package cn.agrael.struts.plugin.ejb3;

import java.util.Map;

import org.apache.struts2.impl.StrutsObjectFactory;

import com.opensymphony.xwork2.config.ConfigurationException;
import com.opensymphony.xwork2.config.entities.InterceptorConfig;
import com.opensymphony.xwork2.interceptor.Interceptor;

/**
 * 基于 EJB3.0 功能的 ObjectFactory 实现。
 * 
 * @author <a href="mailto:agraellee@gmail.com">Agrael·Lee</a>
 * 
 */
public class EJBObjectFactory extends StrutsObjectFactory {

	/** 版本号 */
	private static final long serialVersionUID = 4965387125699005124L;

	@SuppressWarnings("unchecked")
	@Override
	public Interceptor buildInterceptor(InterceptorConfig interceptorConfig, Map refParams) {
		Interceptor interceptor = super.buildInterceptor(interceptorConfig, refParams);
		try {
			// 执行 EJB 的注解
			BeanEJBAnnotation beanEJBAnnotation = StrutsEJBUtils.executeEJBInjectAnnotation(interceptor);
			if (!beanEJBAnnotation.isEmpty()) {
				// 如果注解信息不为空，模拟生命周期方法
				StrutsEJBUtils.simulation(interceptor, beanEJBAnnotation);
				// 则创建代理执行
				interceptor = StrutsEJBUtils.createInterceptorsAnnotationProxy(interceptor, beanEJBAnnotation);
			}
		} catch (Exception e) {
			throw new ConfigurationException(e);
		}
		return interceptor;
	}

}
